/*
 * Copyright (C) 2014 Martin Jones <martin.jones@jolla.com>
 *
 * You may use this file under the terms of the BSD license as follows:
 *
 * "Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Nemo Mobile nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 */

#ifndef SEASIDEVCARDMODEL_H
#define SEASIDEVCARDMODEL_H

#include <QAbstractListModel>
#include <QtQml>
#include <seasidecache.h>
#include <seasideperson.h>
#include <QContact>

#ifdef HAS_MLITE
#include <mgconfitem.h>
#endif

QTCONTACTS_USE_NAMESPACE

class SeasideVCardModel : public QAbstractListModel, public QQmlParserStatus
{
    Q_OBJECT
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(DisplayLabelOrder displayLabelOrder READ displayLabelOrder NOTIFY displayLabelOrderChanged)
    Q_PROPERTY(int count READ count NOTIFY countChanged)
    Q_PROPERTY(QString defaultCodec READ defaultCodec WRITE setDefaultCodec NOTIFY defaultCodecChanged)
    Q_ENUMS(VCardRoles)

public:
    SeasideVCardModel(QObject *parent = 0);
    ~SeasideVCardModel();

    enum DisplayLabelOrder {
        FirstNameFirst = SeasideCache::FirstNameFirst,
        LastNameFirst = SeasideCache::LastNameFirst
    };

    enum VCardRoles {
        PrimaryNameRole = Qt::UserRole,
        SecondaryNameRole,
        AvatarRole,
        AvatarUrlRole,
        PhoneNumbersRole,
        EmailAddressesRole,
        NicknameDetailsRole,
        PhoneDetailsRole,
        EmailDetailsRole,
        PersonRole
    };

    QUrl source() const;
    void setSource(const QUrl &data);

    int count() const;

    QString defaultCodec() const;
    void setDefaultCodec(const QString &codec);

    DisplayLabelOrder displayLabelOrder() const;

    virtual QHash<int, QByteArray> roleNames() const;
    virtual int rowCount(const QModelIndex &) const;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    Q_INVOKABLE SeasidePerson *getPerson(int index) const;

protected:
    void classBegin();
    void componentComplete();

signals:
    void sourceChanged();
    void countChanged();
    void defaultCodecChanged();
    void displayLabelOrderChanged();

private:
    void readContacts();

    bool mComplete;
    QUrl mSource;
    QList<QContact> mContacts;
    mutable QList<SeasidePerson*> mPeople;
#ifdef HAS_MLITE
    MGConfItem mDisplayLabelOrderConf;
#endif
    QString mDefaultCodec;
};

#endif // SEASIDEVCARDMODEL_H
